<template>
  <view>
    <!--使用快捷键 uswiper 生成轮播图的代码片段-->
    <!--轮播图区域  indicator-dots 小圆点，autoplay自动轮播，interval轮播间隔，duration轮播耗时 ，circular轮播衔接        -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,i) in swiperList" :key="i">
        <!--   <view class="swiper-item"> -->
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
        <!--  </view> -->
      </swiper-item>

    </swiper>
    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src" class="nav-img"></image>
        <view>{{item.name}}</view>
      </view>
    </view>
    <!-- 楼层区域 -->
    <!-- 楼层的容器 -->
    <view class="floor-list">
      <!-- 每一个楼层的item项 -->
      <view class="floor-item" v-for="(item,i) in floorList" :key="i">
        <!-- 楼层的标题 -->
        <view>{{item.floor_title.name}}</view>
        <image :src="item.floor_title.image_src" class="floor-title"></image>

        <!--        楼层图片区域 -->
        <view class="floor-img-box">
          <!--          左侧大图片的盒子 -->
          <!--   <view class="left-img-box">
              mode="widthFix" mode属性高度自适应 
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width +'rpx'}"
              mode="widthFix">
            </image>
          </view> -->
          <!--        右侧 4 个小图片的盒子  v-if="i2 !== 0" 跳过第一个元素 视频写到view标签上，但是Vue3这里写到image标签上-->
          <view class="right-img-box">
            <navigator class="right-img-item" v-for="(item2,i2) in item.product_list" :key="i2" :url="item2.url">
              <!--               {{ 'Index: ' + i2 }}  -->
              <image v-if="i2 !== 0" :src="item2.image_src" :style="{width: item2.image_width +'rpx'}" mode="widthFix">
              </image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //这是轮播图的数据列表
        swiperList: [],
        //分类导航的数据列表
        navList: [],
        //1.楼层的数据列表
        floorList: [],
      };
    },

    mounted() {
      if (typeof uni.$showMsg === 'function') {
        console.log('uni.$showMsg is defined and ready to use.');
        uni.$showMsg()
      } else {
        console.error('uni.$showMsg is not defined.');
      }
    },

    onLoad() {
      //  uni.$showMsg('页面加载成功', 'success');
      //调用方法，获取轮播图的数据
      this.getSwiperList()
      //在 onload中调用获取数据的方法
      this.getNavList()
      //在onLoad中调用获取楼层数据的方法
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        // const res =  await uni.$http.get('/api/public/v1/home/swiperdata')
        // console.log(res)
        //const {data: res} =  await uni.$http.get('/api/public/v1/home/swiperdata')
        // if (res.meta.status !== 200) return uni.$showMsg()
        //res.message
        this.swiperList = [{
            "image_src": "https://img0.baidu.com/it/u=928994413,1812933179&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067",
            "open_type": "navigate",
            "goods_id": 129,
            "navigator_url": "/pages/goods_detail/main?goods_id=129"
          },
          {
            "image_src": "https://pic.rmb.bdstatic.com/bjh/40510be6e8d2/240930/4c29ad7a06cc6696241c2570ea03f343.jpg?for=bg",
            "open_type": "navigate",
            "goods_id": 395,
            "navigator_url": "/pages/goods_detail/main?goods_id=395"
          },
          {
            "image_src": "https://img2.baidu.com/it/u=1370830944,3946629041&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=800",
            "open_type": "navigate",
            "goods_id": 38,
            "navigator_url": "/pages/goods_detail/main?goods_id=38"
          }
        ]
        // uni.$showMsg('数据请求成功！')
      },
      async getNavList() {
        // const {data:res} = await uni.$http.get('/api/public/v1/home/catitems')
        //const {data:res} = await uni.$http.get('www.baidu.com')
        //if (res1.meta.status !== 200) return uni.$showMsg()
        //this.navList = res1.message
        this.navList = [{
            "name": "分类",
            "image_src": "/static/my-xrmj/mj2.png",
            "open_type": "switchTab",
            "navigator_url": "/pages/category/main"
          },
          {
            "name": "秒杀拍",
            "image_src": "/static/my-xrmj/mj1.png"
          },
          {
            "name": "超市购",
            "image_src": "/static/my-xrmj/mjy.png"
          },
          {
            "name": "母婴品",
            "image_src": "/static/my-xrmj/gzmj.png"
          }
        ]
      },
      //nav-item 项被点击时的事件处理函数
      navClickHandler(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      //定义获取楼层数据的方法
      async getFloorList() {
        // const{data: res} =  await uni.$http.get('/api/public/v1/home/floordata')
        const res2 = {
          "message": [{
            "floor_title": {
              "name": "简约版",
              // "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_title.png",
              "image_src": "/static/my-xrmj/floorlist1.jpg"
            },
            "product_list": [{
                "name": "优质服饰",
                "image_src": "/static/my-xrmj/zuo.jpg",
                "image_width": "300",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=服饰"
              },
              {
                "name": "春季热门",
                "image_src": "/static/my-xrmj/shang1.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=热"
              },
              {
                "name": "爆款清仓",
                "image_src": "/static/my-xrmj/shang2.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=爆款"
              },
              {
                "name": "倒春寒",
                "image_src": "/static/my-xrmj/xia1.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=春季"
              },
              {
                "name": "怦然心动",
                "image_src": "/static/my-xrmj/xia2.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=心动"
              }
            ]
          }, {
            "floor_title": {
              "name": "简约版222",
              // "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_title.png",
              "image_src": "/static/my-xrmj/floorlist1.jpg"
            },
            "product_list": [{
                "name": "优质服饰",
                "image_src": "/static/my-xrmj/zuo.jpg",
                "image_width": "300",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=服饰"
              },
              {
                "name": "春季热门",
                "image_src": "/static/my-xrmj/shang1.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=热"
              },
              {
                "name": "爆款清仓",
                "image_src": "/static/my-xrmj/shang2.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=爆款"
              },
              {
                "name": "倒春寒",
                "image_src": "/static/my-xrmj/xia1.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=春季"
              },
              {
                "name": "怦然心动",
                "image_src": "/static/my-xrmj/xia2.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=心动"
              }
            ]
          }, {
            "floor_title": {
              "name": "简约版333",
              // "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_title.png",
              "image_src": "/static/my-xrmj/floorlist1.jpg"
            },
            "product_list": [{
                "name": "优质服饰",
                "image_src": "/static/my-xrmj/zuo.jpg",
                "image_width": "300",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=服饰"
              },
              {
                "name": "春季热门",
                "image_src": "/static/my-xrmj/shang1.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=热"
              },
              {
                "name": "爆款清仓",
                "image_src": "/static/my-xrmj/shang2.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=爆款"
              },
              {
                "name": "倒春寒",
                "image_src": "/static/my-xrmj/xia1.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=春季"
              },
              {
                "name": "怦然心动",
                "image_src": "/static/my-xrmj/xia2.jpg",
                "image_width": "185",
                "open_type": "navigate",
                "navigator_url": "/pages/goods_list?query=心动"
              }
            ]
          }],
          "meta": {
            "msg": "获取成功",
            "status": 200
          }
        }
        console.log("floorList:1" + res2.message)
        if (res2.meta.status !== 200) return uni.$showMsg()
        res2.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            //挂载一个自定义的属性 url
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })

        this.floorList = res2.message
        console.log("floorList:2 "+this.floorList)
      },

    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    //利用类选择器和标签选择器让轮播图充满整个区域
    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }

  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }

  }


  .floor-title {
    width: 100%;
    height: 60rpx;
  }

  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .floor-img-box {
    display: flex;
    padding-left: 10rpx;
  }
</style>